package com.rem.rabbitmq.ee.HdeadLetterQueue;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rem.rabbitmq.ee.RabbitMqUtils;

import java.util.HashMap;
import java.util.Map;


/**
 * 普通消费者-验证队列最大长度
 *
 * @author Rem
 * @date 2021-12-27
 */

public class ConsumerMaxLength17 {

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqUtils.getChannel();

        //声明死信交换机
        channel.exchangeDeclare(RabbitMqUtils.EXCHANGE_DEAD, BuiltinExchangeType.DIRECT);
        //声明死信队列
        channel.queueDeclare(RabbitMqUtils.QUEUE_DEAD, false, false, false, null);
        //死信队列绑定死信交换机与routingkey
        channel.queueBind(RabbitMqUtils.QUEUE_DEAD, RabbitMqUtils.EXCHANGE_DEAD, RabbitMqUtils.ROUTING_KEY_DEAD);

        /********************************************************************************/

        //声明普通交换机
        channel.exchangeDeclare(RabbitMqUtils.EXCHANGE_NORMAL_MAXLENGTH, BuiltinExchangeType.DIRECT);
        //声明普通队列 并且绑定死信交换机
        //设置参数
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", RabbitMqUtils.EXCHANGE_DEAD);
        arguments.put("x-dead-letter-routing-key", RabbitMqUtils.ROUTING_KEY_DEAD);
        /**
         * 设置队列最大长度
         */
        arguments.put("x-max-length", 6);
        channel.queueDeclare(RabbitMqUtils.QUEUE_NORMAL_MAXLENGTH, false, false, false, arguments);

        //普通交换机与普通队列通过普通routingkey 进行绑定
        channel.queueBind(RabbitMqUtils.QUEUE_NORMAL_MAXLENGTH, RabbitMqUtils.EXCHANGE_NORMAL_MAXLENGTH, RabbitMqUtils.ROUTING_KEY_NORMAL_MAXLENGTH);

        //消费回调
        DeliverCallback deliverCallback = (consumerTag, message) -> System.out.println("接收到的消息：" + new String(message.getBody()));

        //取消消费回调 如果在消费的时候队列被删除了
        CancelCallback cancelCallback = consumerTag -> System.out.println("消费消息被中断");

        System.out.println("消费者-NORMAL-MaxLength 等待消费");
        channel.basicConsume(RabbitMqUtils.QUEUE_NORMAL_MAXLENGTH, true, deliverCallback, cancelCallback);
    }


}
